// pages/shouye/index/index.js
const app = getApp();
var url = app.globalData.url;
var imgURL = app.globalData.imgUrl; 
var util = require('../../../utils/util.js');
// var QQMapWX = require('../../../utils/qqmap-wx-jssdk.js');
// var qqmapsdk;
Page({
  
  /**
   * 页面的初始数据
   */
  data: {
    latitude: "",
    longitude: "",
    logs: [],
    address: {},
    userid: '',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    interval: 3000,
    duration: 1000,
    vertical: false,
    indicatordots: true,
    autoplay: true,
    type: [
      {
        typename: '早餐',
        img:"../../../image/breakfast.png",
        imgColor: "../../../image/breakfast.png",
      },
      {
        typename: '午餐',
        img: "../../../image/lunch.png",
        imgColor: "../../../image/lunch.png",
      },
      {
        typename: '晚餐',
        img:  "../../../image/dinner.png",
        imgColor:  "../../../image/dinner.png",
      },
      {
        typename: '宵夜',
        img: "../../../image/supper.png",
        imgColor: "../../../image/supper.png",
      },
    ]
  },
  goodsCarNumber: 0,
  banner: function (e) {
    var index = e.currentTarget.dataset.index
    wx.navigateTo({
      url: '../bannerdetail/bannerdetail?index=' + index,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  /**首页购物车跳转 */
  InshoppingCar:function(){
    wx.navigateTo({
      url: '../cart/cart',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  detail: function (e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../listdetail/listdetail?id=' + id,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  today: function () {
    var todayid = 1;
    wx.navigateTo({
      url: '../list/list?todayid=' + todayid,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  tomorrow: function () {
    var tomorrowid = 2;
    wx.navigateTo({
      url: '../list/list?tomorrowid=' + tomorrowid,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  _getUserInfo() {
    wx.getUserInfo({
      success: res => {
        const that = this;
        app.globalData.userInfo = res.userInfo
        console.log(res.userInfo)
        wx.request({
          url: url+'isExitMemberByName',
          data: {
            name: res.userInfo.nickName
          },
          header: {
            "Content-Type": "application/json"
          },
          method: 'GET',
          success: function(res) {
            app.globalData.userid = res.data.object
            console.log(app.globalData.userInfo)
            if (res.data.object == 0){
              wx.request({
                url: url + 'registerByWeChart',
                data: {
                  userName: app.globalData.userInfo.nickName,
                  memberImg: app.globalData.userInfo.avatarUrl
                },
                header: {
                  "Content-Type": "application/json"
                },
                success: function (res) {
                  app.globalData.userid = res.data.object
                  console.log(res)
                  that.setData({
                    userid: res.data.object,
                    members: res.data.object
                  })
                  wx.request({
                    url: url + 'getShopCart',
                    data: {
                      userId: app.globalData.userid,
                      cartType: 1,
                      pageIndex: 1,
                      pageSize: 1000
                    },
                    header: {
                      "Content-Type": "application/json"
                    },
                    success: function (res) {
                      that.goodsCarNumber = 0
                      if (res.data.object.cartList != undefined) {

                        for (let i = 0; i < res.data.object.cartList.length; i++) {
                          that.goodsCarNumber += res.data.object.cartList[i].goodsNumber
                        }
                        that.setData({
                          goodsCarNumber: that.goodsCarNumber
                        })
                      } else {
                        that.setData({
                          goodsCarNumber: 0
                        })
                      }
                    },
                    fail: function (res) { },
                    complete: function (res) { },
                  })
                }
              })
            }else{
              wx.request({
                url: url + 'getShopCart',
                data: {
                  userId: app.globalData.userid,
                  cartType: 1,
                  pageIndex: 1,
                  pageSize: 100
                },
                header: {
                  "Content-Type": "application/json"
                },
                success: function (res) {
                  that.goodsCarNumber = 0
                  if (res.data.object.cartList != undefined) {

                    for (let i = 0; i < res.data.object.cartList.length; i++) {
                      that.goodsCarNumber += res.data.object.cartList[i].goodsNumber
                    }
                    that.setData({
                      goodsCarNumber: that.goodsCarNumber
                    })
                  } else {
                    that.setData({
                      goodsCarNumber: 0
                    })
                  }
                },
                fail: function (res) { },
                complete: function (res) { },
              })
            }
            
          },
          fail: function (res) { },
          complete: function (res) { },
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    

    // 轮播图
    var that = this;
    wx.request({
      url: url + 'getBannerInFront',
      data: {
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        that.setData({
          banner: res.data.object
        })
      }
    })
    //今日用餐
    var date = util.formatTimedate(new Date());
    var day = util.formatTimeday(new Date());
    //明日用餐
    var datet = util.formatTimedatet(new Date());
    var dayt = util.formatTimedayt(new Date());

    if (day == '1' || dayt == '2') {
      day = '一',
        dayt = '二'
    }
    if (day == '2' || dayt == '3') {
      day = '二',
        dayt = '三'
    }
    if (day == '3' || dayt == '4') {
      day = '三',
        dayt = '四'
    }
    if (day == '4' || dayt == '5') {
      day = '四',
        dayt = '五'
    }
    if (day == '5' || dayt == '6') {
      day = '五',
        dayt = '六'
    }
    if (day == '6' || dayt == '7') {
      day = '六',
        dayt = '天'
    }
    if (day == '7' || dayt == '1') {
      day = '天',
        dayt = '一'
    }
    this.setData({
      date: date,
      day: day,
      dayt: dayt,
      datet: datet
    });
    //  //获取查看地理位置
    // wx.getLocation({
    //   type: 'wgs84',
    //   success: function (res) {
    //     console.log(res.latitude);
    //     console.log(res.longitude);
    //     console.log(res.speed);
    //     console.log(res.accuracy);
    //     var latitude = res.latitude
    //     var longitude = res.longitude
    //     var speed = res.speed
    //     var accuracy = res.accuracy
    //     wx.openLocation({
    //       latitude: 32.1625990000,
    //       longitude: 118.6973780000,
    //       scale: 0
    //     })
    //   }

    //  })
    //热销菜品
    this.loadData();
    
  },
  loadData: function () {
    var that = this;
    wx.request({
      url: url + 'getHotDishers',
      data: {
        pageIndex: 1,
        pageSize: 100
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        that.setData({
          list: res.data.object.hotDishers
        })
      }
    })
  },
  onShow:function(){
    const that = this;
    wx.getSetting({
      success: res => {
        if (!res.authSetting['scope.userInfo']) {
          wx.showModal({
            title: '提示',
            content: '您还未授权此程序',
            showCancel: true,
            cancelText: '取消',
            confirmText: '去授权',
            success: function (res) {
              if (res.confirm) {
                wx.switchTab({
                  url: '../../mine/mine/mine',
                })
              } else {
                wx.showModal({
                  title: '拒绝提示',
                  content: '必须授权登录之后才能继续操作呢，是否重新去授权？',
                  showCancel: true,
                  cancelText: '否',
                  cancelColor: '',
                  confirmText: '是',
                  confirmColor: '',
                  success: function (res) {
                    if (res.confirm) {
                      wx.switchTab({
                        url: '../../mine/mine/mine',
                      })
                    }
                  },
                })
              }
            },
            fail: function (res) { },
            complete: function (res) { },
          })
        } else {
          that._getUserInfo()
        }
      },
      fail: res => {
        wx.showModal({
          title: '提示',
          content: '您还未授权此程序',
          showCancel: true,
          cancelText: '取消',
          confirmText: '去授权',
          success: function (res) {
            if (res.confirm) {

              wx.switchTab({
                url: '../../mine/mine/mine',
              })
            } else {
              wx.showModal({
                title: '拒绝提示',
                content: '必须授权登录之后才能继续操作呢，是否重新去授权？',
                showCancel: true,
                cancelText: '否',
                cancelColor: '',
                confirmText: '是',
                confirmColor: '',
                success: function (res) {
                  if (res.confirm) {
                    wx.switchTab({
                      url: '../../mine/mine/mine',
                    })
                  }
                },
              })
            }
          },
          fail: function (res) { },
          complete: function (res) { },
        })

      }
    })
    // this._getUserInfo()
  }
})